package com.syni.mdd.sjb.common.datatreaure.service.impl.databank.saasminapp;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.config.*;
import com.syni.mdd.sjb.common.datatreaure.service.databank.saasminapp.HomeConfigService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class HomeConfigServiceImpl implements HomeConfigService {
    @Autowired
    private UserRestService userRestService;

    @Override
    public Map<String, Object> getDecorationEntrance(AppidVo appidVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/notAuth/getDecorationConfig/entrance";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",appidVo.getAppid());
            Map<String, Object> resultMap = getReqWithHead(postUrl, appidVo.getToken(), appidVo.getUserId(),true,headMap, new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getDecorationBanner(AppidVo appidVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/notAuth/getDecorationConfig/banner";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",appidVo.getAppid());
            Map<String, Object> resultMap = getReqWithHead(postUrl, appidVo.getToken(), appidVo.getUserId(),true,headMap, new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getDecorationImgNav(AppidVo appidVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/notAuth/getDecorationConfig/imgNav";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",appidVo.getAppid());
            Map<String, Object> resultMap = getReqWithHead(postUrl, appidVo.getToken(), appidVo.getUserId(),true,headMap, new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> setEntrance(SetEntranceRqVo setEntranceRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/config/setEntrance?entrance=" + setEntranceRqVo.getEntrance();
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",setEntranceRqVo.getAppid());
            Map<String, Object> resultMap = postReqMember(postUrl, setEntranceRqVo.getToken(), setEntranceRqVo.getUserId(), true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> setImgNav(SetImgNavRqVo setImgNavRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/config/setImgNav";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",setImgNavRqVo.getAppid());
            Map<String, Object> rqMap = new HashMap<>();
            rqMap.put("imgNavConfigData",setImgNavRqVo.getImgNavConfigData());
            Map<String, Object> resultMap = postReqMember(postUrl, setImgNavRqVo.getToken(), setImgNavRqVo.getUserId(), true,headMap,rqMap);
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> setBanner(SetBannerVo setBannerVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/config/setBanner";
            Map<String, Object> rqMap = new HashMap<>();
            rqMap.put("bannerConfigData",setBannerVo.getBannerConfigData());
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid",setBannerVo.getAppid());
            Map<String, Object> resultMap = postReqMember(postUrl, setBannerVo.getToken(), setBannerVo.getUserId(), true,headMap,rqMap);
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getPageJson(StoreIdVo storeIdVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/notAuth/getPageJson";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId", storeIdVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, storeIdVo.getToken(), storeIdVo.getUserId(), true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getStoreStyle(AppidVo appidVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/theme/get";
            Map<String,Object> params = new HashMap<>();
            params.put("appid", appidVo.getAppid());
            Map<String, Object> resultMap = reqMember(postUrl, appidVo.getToken(), appidVo.getUserId(), params);
            if(resultMap.get("code") != null && resultMap.get("code").toString().equals("200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> saveStoreStyle(SaveStoreStyleVo saveStoreStyleVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/theme/save";
            Map<String,Object> params = new HashMap<>();
            params.put("appid", saveStoreStyleVo.getAppid());
            params.put("theme", saveStoreStyleVo.getTheme());
            Map<String, Object> resultMap = postReqMember(postUrl, saveStoreStyleVo.getToken(), saveStoreStyleVo.getUserId(),false,new HashMap<>(), params);
            if(resultMap.get("code") != null && resultMap.get("code").toString().equals("200")){
                return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> getNavigation(AppidVo appidVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/notAuth/getDecorationConfig/tile";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid", appidVo.getAppid());
            Map<String, Object> resultMap = getReqWithHead(postUrl, appidVo.getToken(), appidVo.getUserId(),true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    @Override
    public Map<String, Object> setNavigation(SetNavigationVo setNavigationVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/applet/config/setTile";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("appid", setNavigationVo.getAppid());
            Map<String,Object> params = new HashMap<>();
            params.put("tileConfigData",setNavigationVo.getTileConfigData());
            Map<String, Object> resultMap = postReqMember(postUrl, setNavigationVo.getToken(), setNavigationVo.getUserId(),true,headMap,params);
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }


    public Map<String,Object> reqMember(String requestUrl,String token,Integer userId,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,RestTemplateUtils.getSassHeader(minitoken));
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> getReqWithHead(String requestUrl,String token,Integer userId,Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postFormReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntity(RestTemplateUtils.getLoadRestTemplate(),requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }
}
